package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle.pojo.dto.UserRegParam;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

import static cn.tedu._03vehicle.base.response.JsonResult.ok;

@Api(tags = "01.用户管理")
@Slf4j
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户注册功能模块
     * @param userRegParam
     * @return
     */
    @PostMapping("reg")
    @ApiOperation(value = "用户注册")
    @ApiOperationSupport(order = 10)
    public JsonResult regUser(@RequestBody  @Validated UserRegParam userRegParam){
        log.debug("开始进行用户注册:"+userRegParam);
        String username = userRegParam.getUsername();
        int i = userMapper.selectUser(username);
        if(i>0){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int num = userMapper.insertUser(user);
        if(num>0){
            return JsonResult.ok(userRegParam);
        }else{
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    /**
     * 用户登录模块
     * @param userLoginParam
     * @param session
     * @return
     */
    @PostMapping("login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam,@ApiIgnore HttpSession session){
        log.debug("开始进行用户登录:"+userLoginParam);
        UserVO userVO = userMapper.selectlogin(userLoginParam);
//        int i = userMapper.selectlogin(username,password);
//        if(i>0){
//            return new JsonResult(StatusCode.OPERATION_SUCCESS,userLoginParam);
//        }else{
//            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
//        }
        log.debug("userVO="+userVO);
        if(userVO!=null){
            //将用户信息存入到session会话当中
            session.setAttribute("user",userVO);
            return JsonResult.ok(userVO);
        }else {
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
    }
    /*
    3.获取当前用户的信息功能
    获取用户信息功能模块
     */
    @GetMapping("currentUser")
    @ApiOperation("获取当前用户的信息功能")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        //在session中获取会话标识
        /*
            1.userVO: null
            2.userVO: {"id":100, "username":"Lucy"}
         */
         /*
          1.服务端返回的数据response: {"state":2000,"message":"操作成功", "data": userVO}
          2.前端工程师:
            2.1 从响应中获取到 userVO;
            2.2 利用JavaScript代码进行判断;
                if(userVO == null){//未登录
                    response.redirect("/login.html"); //重定向
                } else{//登录状态
                    var username = response.data.data.get("username");
                    把username渲染到页面上,比如:欢迎你:xxx
                }
         */
        UserVO userVO = (UserVO) session.getAttribute("user");

        return JsonResult.ok(userVO);

    }

    /**
     *用户退出登录模块
     * @param session
     */
    @GetMapping("logout")
    @ApiOperation("用户退出登录")
    @ApiOperationSupport(order = 40)
    public void logout(@ApiIgnore HttpSession session){
        //在session中移除会话标识
        session.removeAttribute("user");
    }

}
